<?php

/**
 *    商品属性管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class AttributeApp extends BackendApp
{
	var $_attribute_mod;
	function __construct()
	{
		$this->AttributeApp();
	}

	function AttributeApp()
	{
		parent::BackendApp();

		$this->_attribute_mod =& m('attribute');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
			
		$conditions='';
        $conditions = $this->_get_query_conditions(array(array(
            'field' => 'attr_name',       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'attr_name',
        	),
        ));
		
		
		$page   =   $this->_get_page(10);   //获取分页信息
		$attributes=$this->_attribute_mod->find(array(
        'fields'   => ' * ',
        'conditions'  => ' 1=1 ' . $conditions,
        'limit'   => $page['limit'],
        'order'   => ' sort_order DESC', //必须加别名
        'count'   => true   //允许统计
		));    //找出所有的文章

		$page['item_count']=$this->_attribute_mod->getCount();   //获取统计数据

			
		$this->_format_page($page);
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件

		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('attributes', $attributes);
		$this->display('attribute.index.html');
	}
	/**
	 *    新增文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function add()
	{
		if (!IS_POST)
		{
			/* 显示新增表单 */

			$attribute = array('sort_order' => 255);
			
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->assign('attribute', $attribute);
			$this->display('attribute.form.html');
		}
		else
		{
			$data = array();
			$data['attr_name']      =   $_POST['attr_name'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['def_value'] =   $_POST['def_value'];
			$data['input_mode'] =   $_POST['input_mode'];

			if (!$attr_id = $this->_attribute_mod->add($data))  //获取article_id
			{
				$this->show_warning($this->_attribute_mod->get_error());
				return;
			}		

			$this->show_message('add_attribute_successed',
                'back_list',    'index.php?app=attribute',
                'continue_add', 'index.php?app=attribute&amp;act=add'
            );
		}
	}
	/**
	 *    编辑文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function edit()
	{
		$attr_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$attr_id)
		{
			$this->show_warning('no_such_attribute');
			return;
		}
		if (!IS_POST)
		{
			$attribute = $this->_attribute_mod->find($attr_id);

			if (empty($attribute))
			{
				$this->show_warning('no_such_attribute');
				return;
			}
			
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$attribute = current($attribute);
			$this->assign('attribute', $attribute);
			$this->display('attribute.form.html');
		}
		else
		{
			$data = array();
			$data['attr_name']      =   $_POST['attr_name'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['def_value'] =   $_POST['def_value'];
			$data['input_mode'] =   $_POST['input_mode'];

			$rows=$this->_attribute_mod->edit($attr_id, $data);
			if ($this->_attribute_mod->has_error())
			{
				$this->show_warning($this->_attribute_mod->get_error());
				return;
			}

			$this->show_message('edit_attribute_successed',
                'back_list',        'index.php?app=attribute',
                'edit_again',    'index.php?app=attribute&amp;act=edit&amp;id=' . $attr_id);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('sort_order')))
		{
			$data[$column] = $value;
			$this->_attribute_mod->edit($id, $data);
			if(!$this->_attribute_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	function drop()
	{
		$attr_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$attr_ids)
		{
			$this->show_warning('no_such_attribute');
			return;
		}
		$attr_ids=explode(',', $attr_ids);
		$this->_attribute_mod->drop($attr_ids);
		if ($this->_attribute_mod->has_error())    //删除
		{
			$this->show_warning($this->_attribute_mod->get_error());

			return;
		}

		$this->show_message('drop_ok');
	}
}